﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FIFP.Common;
using FIFP.DashBoard.Helper;
using FIFP.DashBoard.Views;
using FIFP.DashBoard.Model;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using FIFP.DashBoard.Converters;

namespace FIFP.DashBoard.ViewModel
{
    public class MainPageVM : ViewModelCore
    {
        #region Variables
        public event EventHandler OnLinkedShutdownClick;
        public event EventHandler OnOverlapClick;
        public event EventHandler OnImportClick;
        public event EventHandler OnOptimizationClick;
        public event EventHandler OnScenarioClick;
        #endregion

        #region Properties
        private Grid mainGrid;
        public Grid MainGrid
        {
            get { return mainGrid; }
            set { SetPropertyValue(() => MainGrid, value, val => mainGrid = val); }
        }

        private bool _IsImportChecked;
        public bool IsImportChecked
        {
            get { return _IsImportChecked; }
            set { SetPropertyValue(() => IsImportChecked, value, val => _IsImportChecked = val); }
        }

        private bool _IsOptimizationChecked;
        public bool IsOptimizationChecked
        {
            get { return _IsOptimizationChecked; }
            set { SetPropertyValue(() => IsOptimizationChecked, value, val => _IsOptimizationChecked = val); }
        }

        private bool _IsScenarioChecked;
        public bool IsScenarioChecked
        {
            get { return _IsScenarioChecked; }
            set { SetPropertyValue(() => IsScenarioChecked, value, val => _IsScenarioChecked = val); }
        }

        private Visibility? _IsSubMenuVisible = Visibility.Collapsed;
        public Visibility? IsSubMenuVisible
        {
            get { return _IsSubMenuVisible; }
            set { SetPropertyValue(() => IsSubMenuVisible, value, val => _IsSubMenuVisible = val); }
        }


        private bool _IsOverLapChecked = true;
        public bool IsOverLapChecked
        {
            get { return _IsOverLapChecked; }
            set { SetPropertyValue(() => IsOverLapChecked, value, val => _IsOverLapChecked = val); }
        }

        private bool _IsLinkedSDChecked;
        public bool IsLinkedSDChecked
        {
            get { return _IsLinkedSDChecked; }
            set { SetPropertyValue(() => IsLinkedSDChecked, value, val => _IsLinkedSDChecked = val); }
        }

        private bool _IsOptMngChecked;
        public bool IsOptMngChecked
        {
            get { return _IsOptMngChecked; }
            set { SetPropertyValue(() => IsOptMngChecked, value, val => _IsOptMngChecked = val); }
        }

        private Style _SaveAsStyle = Application.Current.Resources[FIFPConstants.BUTTON_DEFAULTCOLOR] as Style;
        public Style SaveAsStyle
        {
            get { return _SaveAsStyle; }
            set { SetPropertyValue(() => SaveAsStyle, value, val => _SaveAsStyle = val); }
        }

        private Style _SaveStyle = Application.Current.Resources[FIFPConstants.BUTTON_DEFAULTCOLOR] as Style;
        public Style SaveStyle
        {
            get { return _SaveStyle; }
            set { SetPropertyValue(() => SaveStyle, value, val => _SaveStyle = val); }
        }
        private bool _IsIntegrationEnabled;
        public bool IsIntegrationEnabled
        {
            get { return _IsIntegrationEnabled; }
            set { SetPropertyValue(() => IsIntegrationEnabled, value, val => _IsIntegrationEnabled = val); }
        }

        private bool isLSDEnabled = true;
        public bool IsLSDEnabled
        {
            get { return isLSDEnabled; }
            set { SetPropertyValue(() => IsLSDEnabled, value, val => isLSDEnabled = val); }
        }

        #endregion

        #region Commands

        private ICommand _NavigationCommand;
        public ICommand NavigationCommand
        {
            get
            {
                return _NavigationCommand ??
                        (_NavigationCommand = new RelayCommand<RadioButton>(Navigation));
            }
        }

        private ICommand _PageLoadCommand;
        public ICommand PageLoadCommand
        {
            get
            {
                return _PageLoadCommand ??
                        (_PageLoadCommand = new RelayCommand<Grid>(InitializePage));
            }
        }

        private ICommand _SaveCommand;
        public ICommand SaveCommand
        {
            get
            {
                return _SaveCommand ??
                        (_SaveCommand = new RelayCommand<Button>(DoSaveButtonLogic));
            }
        }


        #endregion

        #region Constructor
        public MainPageVM()
        {
        }
        #endregion

        #region Methods

        #region InitializePage
        private void InitializePage(Grid layoutRoot)
        {
            if (MainGrid == null)
            {
                MainGrid = layoutRoot;
                IsImportChecked = true;
                if (MainGrid != null && MainGrid.Children != null && MainGrid.Children.Count > 0)
                    MainGrid.Children.Clear();
                MainGrid.Children.Add(ServiceLocator.Get<Import>());
            }
        }
        #endregion

        #region Navigation
        private void Navigation(RadioButton rdBtn)
        {
            IsImportChecked = IsOptimizationChecked = IsScenarioChecked = false;
            //if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_SHUTDOWN && ServiceLocator.Get<OverlapVM_New>().OverlapOriginalPlans != null && ServiceLocator.Get<OverlapVM_New>().OverlapOriginalPlans.Where(o => o.IsOverlapped == true).Count() > 0)
            //{
            //    Dispatcher.BeginInvoke(() =>
            //    {
            //        MessageBox.Show("still overlap");
            //        IsOptimizationChecked = true;
            //        IsImportChecked = IsScenarioChecked = false;
            //        IsSubMenuVisible = Visibility.Visible;
            //        IsOverLapChecked = true;
            //        IsLinkedSDChecked = IsOptMngChecked = false;
            //    });
            //}
            //else
            {
                if (MainGrid != null && MainGrid.Children != null && MainGrid.Children.Count > 0)
                    MainGrid.Children.Clear();
                if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_IMPORT)
                {
                    MainGrid.Children.Add(ServiceLocator.Get<Import>());
                    IsImportChecked = true;
                    IsOptimizationChecked = IsScenarioChecked = false;
                    IsSubMenuVisible = Visibility.Collapsed;
                    ServiceLocator.Get<FilterVM>().IsDateTimeVisibility = Visibility.Visible;
                    ServiceLocator.Get<FilterVM>().IsCombinedTowerVisibility = Visibility.Collapsed;
                    if (OnImportClick != null)
                        OnImportClick(this, null);
                }
                else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_OPTIMIZATION)
                {
                    if (IsOverLapChecked)
                    {                        
                        MainGrid.Children.Add(ServiceLocator.Get<OverlapManagement_New>());
                        if (OnOverlapClick != null)
                            OnOverlapClick(this, null);
                    }
                    else if (IsLinkedSDChecked)
                    {
                        MainGrid.Children.Add(ServiceLocator.Get<LSDManagement>());
                        if (OnLinkedShutdownClick != null)
                            OnLinkedShutdownClick(this, null);
                    }
                    else
                    {
                        MainGrid.Children.Add(ServiceLocator.Get<OptManagement_New>());
                        if (OnOptimizationClick != null)
                            OnOptimizationClick(this, null);
                    }
                    IsOptimizationChecked = true;
                    IsImportChecked = IsScenarioChecked = false;
                    IsSubMenuVisible = Visibility.Visible;
                }
                else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_OVERLAP)
                {
                    MainGrid.Children.Add(ServiceLocator.Get<OverlapManagement_New>());
                    IsOptimizationChecked = true;
                    IsImportChecked = IsScenarioChecked = false;
                    IsSubMenuVisible = Visibility.Visible;
                    IsOverLapChecked = true;
                    IsLinkedSDChecked = IsOptMngChecked = false;
                    if (OnOverlapClick != null)
                        OnOverlapClick(this, null);
                }
                else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_OPTMNG)
                {
                    MainGrid.Children.Add(ServiceLocator.Get<OptManagement_New>());
                    IsOptimizationChecked = true;
                    IsImportChecked = IsScenarioChecked = false;
                    IsSubMenuVisible = Visibility.Visible;
                    IsOptMngChecked = true;
                    IsLinkedSDChecked = IsOverLapChecked = false;
                    ServiceLocator.Get<FilterVM>().IsDateTimeVisibility = Visibility.Collapsed;
                    ServiceLocator.Get<FilterVM>().IsCombinedTowerVisibility = Visibility.Collapsed;
                    if (OnOptimizationClick != null)
                        OnOptimizationClick(this, null);
                }
                else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_SHUTDOWN)
                {
                    MainGrid.Children.Add(ServiceLocator.Get<LSDManagement>());
                    IsOptimizationChecked = true;
                    IsImportChecked = IsScenarioChecked = false;
                    IsSubMenuVisible = Visibility.Visible;
                    IsLinkedSDChecked = true;
                    IsOptMngChecked = IsOverLapChecked = false;
                    ServiceLocator.Get<FilterVM>().IsDateTimeVisibility = Visibility.Collapsed;
                    ServiceLocator.Get<FilterVM>().IsCombinedTowerVisibility = Visibility.Visible;
                    if (OnLinkedShutdownClick != null)
                        OnLinkedShutdownClick(this, null);
                }
                else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_SCENARIO)
                {
                    MainGrid.Children.Add(ServiceLocator.Get<Scanario>());
                    IsScenarioChecked = true;
                    IsImportChecked = IsOptimizationChecked = false;
                    IsSubMenuVisible = Visibility.Collapsed;
                    if (OnScenarioClick != null)
                        OnScenarioClick(this, null);
                }
            }
            // Commented by Dhilip for Navigation with Page refreshing logic
            /*            
            string queryString = string.Empty;            
            if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_IMPORT)
            {                
                queryString = FIFPConstants.NAVIGATION_TYPE + FIFPConstants.HYPERLINK_IMPORT;
            }
            else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_OPTIMIZATION)
            {                
                queryString = FIFPConstants.NAVIGATION_TYPE + FIFPConstants.HYPERLINK_OPTIMIZATION;
            }
            else if (rdBtn.Tag.ToString() == FIFPConstants.HYPERLINK_SCENARIO)
            {   
                queryString = FIFPConstants.NAVIGATION_TYPE + FIFPConstants.HYPERLINK_SCENARIO;
            }
             
            string url = HtmlPage.Document.DocumentUri.AbsoluteUri;
            url = url.Substring(0, url.IndexOf(HtmlPage.Document.DocumentUri.LocalPath));
            string pageName = string.Empty;

            pageName = (App.CustomPageName != String.Empty ? App.CustomPageName : FIFPConstants.ASPX_PAGE);
            Uri b = new Uri(String.Format("{0}{1}", url.ToString(), "/" + pageName + (pageName.Contains("?") ? "&" : "?") + queryString));
            HtmlPage.Window.Navigate(b, "_parent");
            
            */
        }
        #endregion

        private void DoSaveButtonLogic(Button btnSave)
        {
            string tag = string.Empty;
            if (btnSave != null && btnSave.Tag != null)
                tag = Convert.ToString(btnSave.Tag);
            switch (tag)
            {

                case FIFPConstants.HYPERLINK_SAVEAS:
                    SaveAsStyle = Application.Current.Resources[FIFPConstants.BUTTON_SELECTEDCOLOR] as Style;
                    SaveStyle = Application.Current.Resources[FIFPConstants.BUTTON_DEFAULTCOLOR] as Style;
                    break;
                case FIFPConstants.HYPERLINK_SAVE:
                    SaveStyle = Application.Current.Resources[FIFPConstants.BUTTON_SELECTEDCOLOR] as Style;
                    SaveAsStyle = Application.Current.Resources[FIFPConstants.BUTTON_DEFAULTCOLOR] as Style;
                    break;
            }
        }

        #endregion
    }
}
